import 'package:flutter/material.dart';

import 'package:postx/src/services/sendsrv.dart';
import 'package:postx/src/commponents/complugin.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool loading = true;
  ValueNotifier<res> response = ValueNotifier<res>(res("无请求", null, null));
  selectView(IconData icon, String text, String id) {
    return PopupMenuItem<String>(
        value: id,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Icon(icon, color: Colors.blue),
            Text(text),
          ],
        ));
  }

  void showHelp(context) {
    showMaterialDialogSingle(context, '''
    API请求请使用如下格式:
    1.调用方法和URL中间空格
    2.请求头第二行开始，之间没有空格
    3.请求头和body之间空一行 \n
    demo: \n
    POST http://abc.com\n
    version: 1\n
    
    {"aaa":"bbb"}\n''');
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final TextEditingController sendinfo = TextEditingController();
    btnSend() async {
      var responseT = await sendsrv(sendinfo.text);
      /*  setState(() {
        response = responseT.toString();
      }); */
      print(responseT.dataJson);
      response.value = responseT;
    }

    return MaterialApp(
      home: DefaultTabController(
        length: 1,
        child: Scaffold(
            appBar: AppBar(
                leading: PopupMenuButton<String>(
                  itemBuilder: (BuildContext context) =>
                      <PopupMenuItem<String>>[
                    selectView(Icons.message, '打开文件', 'A'),
                    selectView(Icons.group_add, '保存文件', 'B'),
                    selectView(Icons.cast_connected, '退出', 'C'),
                  ],
                  onSelected: (String action) {
                    // 点击选项的时候
                    switch (action) {
                      case 'A':
                        break;
                      case 'B':
                        break;
                      case 'C':
                        break;
                    }
                  },
                ),
                title: const Text('POSTX')),
            body: Center(
              child: Column(
                children: [
                  const SizedBox(height: 10),
                  Row(
                    children: [
                      const SizedBox(width: 10),
                      MaterialButton(
                        child: const Text('发送'),
                        onPressed: () => btnSend(),
                        color: Colors.lightGreen[300],
                      ),
                      const SizedBox(width: 10),
                      MaterialButton(
                        child: const Text('清空'),
                        onPressed: () => setState(() {}),
                        color: Colors.lightGreen[300],
                      ),
                      const SizedBox(width: 10),
                      MaterialButton(
                        child: const Text('帮助'),
                        onPressed: () => showHelp(context),
                        color: Colors.lightGreen[300],
                      ),
                    ],
                  ),
                  const SizedBox(height: 10),
                  SizedBox(
                    width: 1230.0,
                    height: 200.0,
                    child: Card(
                      color: Colors.black,
                      elevation: 20.0,
                      shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(20.0)),
                      ),
                      clipBehavior: Clip.antiAlias,
                      semanticContainer: false,
                      child: TextField(
                        textInputAction: TextInputAction.newline,
                        style: const TextStyle(
                          fontSize: 20,
                          color: Colors.grey,
                        ),
                        controller: sendinfo,
                        maxLines: 10,
                        decoration: const InputDecoration(
                          hintText: "请输入参数",
                          hintStyle: TextStyle(color: Colors.grey),
                          border: OutlineInputBorder(),
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: 10),
                  SizedBox(
                      width: 1300.0,
                      height: 350.0,
                      child: Card(
                          color: Colors.black,
                          elevation: 20.0,
                          shape: const RoundedRectangleBorder(
                            borderRadius:
                                BorderRadius.all(Radius.circular(20.0)),
                          ),
                          clipBehavior: Clip.antiAlias,
                          semanticContainer: false,
                          margin: const EdgeInsets.all(20.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const SizedBox(
                                height: 10,
                              ),
                      
                              Expanded(                                
                                child: ValueListenableBuilder<res>(
                                  builder: (context, value, _) {
                                    return SelectableText(
                                        '${value.alerMsg}\n消息头: ${value.headers}\n消息体: ${value.dataJson}\n',
                                        textAlign: TextAlign.left,
                                        style: const TextStyle(
                                          fontSize: 20,
                                          color: Colors.white,
                                        ));
                                  },
                                  valueListenable: response,
                                ),
                              ),
                            ],
                          )))
                ],
              ),
            )),
      ),
    );
  }
}
